/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.utils;

import java.awt.*;

/**
 * Created by Peter on 2016/8/4.
 */
public final class WindowUtils {
    private static final double HALF_PERCENT = 0.5D;

    private WindowUtils() {
    }

    /**
     * Set the window to center on screen.
     *
     * @param frame window.
     */
    public static void centerWindowOnScreen(Window frame) {
        positionWindowOnScreen(frame, HALF_PERCENT, HALF_PERCENT);
    }

    /**
     * Set the position of window on screen
     *
     * @param frame             window
     * @param horizontalPercent x percent
     * @param verticalPercent   y percent
     */
    public static void positionWindowOnScreen(Window frame, double horizontalPercent, double verticalPercent) {
        Rectangle s = frame.getGraphicsConfiguration().getBounds();
        Dimension f = frame.getSize();
        int w = Math.max(s.width - f.width, 0);
        int h = Math.max(s.height - f.height, 0);
        int x = (int) (horizontalPercent * (double) w) + s.x;
        int y = (int) (verticalPercent * (double) h) + s.y;
        frame.setBounds(x, y, f.width, f.height);
    }
}
